/**
 * Copyright European Commission's
 * Taxation and Customs Union Directorate-General (DG TAXUD).
 */
package com.eryos.bean;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eryos.Constants;

/**
 * 
 * @author $Author: $
 * @version $Revision: $
 */
public class Report {

    /** The logger. */
    private static final Logger LOGGER = LoggerFactory.getLogger(Report.class);

    private static final String HEADER = "<!DOCTYPE html><html><head><meta charset=\"UTF-8\"></head><body>";
    private static final String FOOTER = "</body></html>";

    private File report = new File("report_1.html");
    private Writer bw;

    private List<City> listOfCities;
    private String level = "";
    private String reportName = "";

    public Report(String reportName, String level) {
        this.level = level;
        this.reportName = reportName;
        report = new File(reportName);
    }

    public void generateReport() throws IOException {
        generateReport(false);
    }

    public void generateReport(boolean filterResults) throws IOException {
        LOGGER.info("Generate report for <{}>", level);

        Collections.sort(listOfCities, new Comparator<City>() {
            @Override
            public int compare(City o1, City o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });

        // LOGGER.info("TC_VATRATES_VALIDATE_1");
        bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(report), "UTF-8"));

        bw.write(HEADER);
        bw.flush();

        bw.write("<a id=\"top\"><h2>Criterias</h2></a>");
        bw.write("<ul>");
        bw.write("<li>Zone : " + level + "</li>");
        bw.write("</ul><hr>");
        bw.flush();

        bw.write("<h2>Cities</h2>");
        bw.write("<ul>");

        for (City city : listOfCities) {

            List<Result> listOfResult = city.getListOfResult();
            if (filterResults) {
                listOfResult = city.getListOfResultToday();
            }

            if (listOfResult.size() > 0) {
                write(city.getSummary(filterResults));
            }
        }

        write("</ul><hr>");

        for (City city : listOfCities) {
            LOGGER.info("Report for <{}>", city.getName());

            List<Result> listOfResult = city.getListOfResult();
            if (filterResults) {
                listOfResult = city.getListOfResultToday();
            }

            if (listOfResult.size() > 0) {
                write(city.getTitle(filterResults));

                for (Result result : listOfResult) {
                    LOGGER.info("Report for <{}> ", result.getTitle());
                    if (result != null) {
                        if ((filterResults && result.getDate().startsWith(Constants.AUJOURD_HUI)) || !filterResults) {
                            write(result.toHTML());
                        }
                    }
                }
            }

        }

        write(FOOTER);
        bw.close();
    }

    public void write(String content) throws IOException {
        if (bw != null) {
            bw.write(content);
            bw.flush();
        }
    }

    /**
     * @return the level
     */
    public String getLevel() {
        return level;
    }

    /**
     * Sets the level.
     * 
     * @param level
     *            the value to set
     */
    public void setLevel(String level) {
        this.level = level;
    }

    /**
     * @return the listOfCities
     */
    public List<City> getListOfCities() {
        return listOfCities;
    }

    public void setListOfCities(List<City> list) {
        listOfCities = list;
    }

    @Override
    public String toString() {
        return reportName;
    }

}
